#include <iostream>
#define LL long long
using namespace std;

const LL kInf64 = 1e18;

LL f[2][10];
LL d1[4], d2[4];

void Renew(LL& a, LL b) {
  a = max(a, b);
}
void Calc() {
  int k;
  LL d[4][4], dd[4][4];

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      d[i][j] = dd[i][j] = -kInf64;
    }
  }
  d[0][0] = 0;
  for (cin >> k; k; k--) {
    int c_i, d_i;

    cin >> c_i >> d_i;
    for (int i = 3; i > 0; i--) {
      for (int j = 3; j >= c_i; j--) {
        Renew(dd[i][j], dd[i - 1][j - c_i] + d_i);
        Renew(dd[i][j], d[i - 1][j - c_i] + d_i * 2);
        Renew(d[i][j], d[i - 1][j - c_i] + d_i);
      }
    }
  }
  for (int i = 0; i < 4; i++) {
    d1[i] = d2[i] = -kInf64;
    for (int j = 0; j < 4; j++) {
      Renew(d1[i], d[i][j]);
      Renew(d2[i], dd[i][j]);
    }
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;

  fill(f[0], f[0] + 10, -kInf64);
  f[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    Calc();
    fill(f[i & 1], f[i & 1] + 10, -kInf64);
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 4; k++) {
        if (j + k < 10) {
          Renew(f[i & 1][j + k], f[(i - 1) & 1][j] + d1[k]);
        } else {
          Renew(f[i & 1][j + k - 10], f[(i - 1) & 1][j] + d2[k]);
        }
      }
    }
  }
  LL ans = f[n & 1][0];
  for (int j = 1; j < 10; j++) {
    Renew(ans, f[n & 1][j]);
  }
  cout << ans << "\n";
  return 0;
}